package com.tenglong.teacher.ui.login;

import android.app.Activity;

import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.requestInfo.WYLoginInfo;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author hdx on 2019/5/30.
 */
public class LoginT {
    private Activity mContext;
    private LoginTListener loginTListener;
    private ClientUserInfo detailUserInfo;
    private WYLogin mWyLogin;

    public LoginT(Activity context, final LoginTListener loginTListener) {
        this.mContext = context;
        this.loginTListener = loginTListener;
        mWyLogin = new WYLogin(new WYLogin.HunXinListener() {
            @Override
            public void onSucceed() {
                DataState data = DataState.getInstance();
                if (detailUserInfo != null) {
                    data.resetLoginInfo(detailUserInfo);
                }
                data.setWYLoginInfo(new WYLoginInfo(data.getUserInfo().getImUserName(), data.getUserInfo().getImUserPassword()));
                if (loginTListener != null) {
                    loginTListener.dissLoadingDialog();
                    loginTListener.loginSucceed();
                }
            }

            @Override
            public void onFailed() {
                if (loginTListener != null) {
                    loginTListener.dissLoadingDialog();
                    loginTListener.showToast(mContext.getString(R.string.login_failed));
                    loginTListener.loginFailed();
                }
            }

            @Override
            public void onChangeParams() {
                ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
                if (detailUserInfo != null && userInfo != null) {
                    detailUserInfo.setImUserName(userInfo.getImUserName());
                    detailUserInfo.setImUserPassword(userInfo.getImUserPassword());
                }
            }
        });
    }

    public void getUserInfos() {
        final ClientUserInfo mUserInfo = DataState.getInstance().getUserInfo();
        if (mUserInfo == null) {
            return;
        }
        if (loginTListener != null) {
            loginTListener.showLoadingDialog();
        }
        mUserInfo.setDetaiInfo(true);
        detailUserInfo = mUserInfo;
        if (mWyLogin != null) {
            mWyLogin.loginNim();
        }
    }

    public interface LoginTListener {
        void showLoadingDialog();

        void dissLoadingDialog();

        void showToast(String message);

        void loginSucceed();

        void loginFailed();
    }
}
